﻿using Client.Views;
using Client.Processes;
using Client.ViewModels;

namespace Client.Commands
{
    class SearchMoviesCommand : BaseCommand
    {
        private ReadMovies reader;

        public SearchMoviesCommand()
        {
            this.reader = new ReadMovies(); 
            this.reader.OnCompleted += m =>
            {
                MainViewModel.Default.ActiveWorkspace.MovieItems = m;
                MainViewModel.Default.ActiveWorkspace.IsBusy = false;
            };
        }

        public override bool CanExecute(object parameter)
        {
            return !MainViewModel.Default.ActiveWorkspace.IsBusy;
        }

        public override void Execute(object parameter)
        {
            var searchView = new SearchMoviesView();
            var result = searchView.ShowDialog();

            if ((bool)result)
            {
                MainViewModel.Default.AddMovieListWorkspace("Suchergebnisse");
                MainViewModel.Default.ActiveWorkspace.IsBusy = true;

                reader.Process();
            }
        }
    }
}
